﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Brain
{
    public class BrainTrackBar
    {
        protected Sprite Track;
        protected Sprite Slider;

        public Rectangle TrackRectangle
        {
            get { return Track.BoundsRectangle; }
            protected set { Track.BoundsRectangle = value; }
        }
        public Rectangle SliderRectangle
        {
            get { return Slider.BoundsRectangle; }
            set { Slider.BoundsRectangle = value; }
        }

        public event SetFloat OnSlide;

        public float Maximum;
        public float Minimum;

        private float currentValue;
        public float CurrentValue
        {
            get { return currentValue; }
            set
            {
                currentValue = MathHelper.Clamp(value, Minimum, Maximum);
                if (UsingX)
                {
                    float End = Maximum - Minimum;
                    float val = value / End;
                    float Dif = MaxX - MinX;
                    SliderRectangle = new Rectangle((int)MathHelper.Clamp((int)(MinX + (Dif * val)),
                        MinX, MaxX), SliderRectangle.Y, SliderRectangle.Width, SliderRectangle.Height);
                }
                else
                {
                    float End = Maximum - Minimum;
                    float val = value / End;
                    float Dif = MaxY - MinY;
                    SliderRectangle = new Rectangle((int)MathHelper.Clamp((int)(MinY + (Dif * val)),
                        MinY, MaxY), SliderRectangle.Y, SliderRectangle.Width, SliderRectangle.Height);
                }
            }
        }

        public BrainTrackBar(int TrackWidth, int TrackHeight, int X, int Y,
            int SliderWidth, int SliderHeight, float Minimum,
            float Maximum)
        {
            Track = new Sprite();
            Track.BoundsRectangleColor = Color.Gray;
            Track.Rendering = SpriteRendering.Rendered;

            Slider = new Sprite();
            Slider.BoundsRectangleColor = Color.White;
            Slider.Rendering = SpriteRendering.Rendered;

            TrackRectangle = new Rectangle(X, Y, TrackWidth, TrackHeight);
            SliderRectangle = new Rectangle(X - (SliderWidth / 2) + (TrackWidth / 2),
                Y - (SliderHeight / 2) + (TrackHeight / 2), SliderWidth, SliderHeight);

            UsingX = TrackRectangle.Width > TrackRectangle.Height;

            this.Minimum = Minimum;
            this.Maximum = Maximum;
        }

        public bool UsingX;

        public float Porcentage
        {
            get
            {
                if (UsingX)
                {
                    float End = MaxX - MinX;
                    float fin = 100 / End;
                    return ((float)CurrentSliderPosition * fin);
                }
                else
                {
                    float End = MaxY - MinY;
                    float fin = 100 / End;
                    return ((float)CurrentSliderPosition * fin);
                }
            }
        }
        public int CurrentSliderPosition
        {
            get
            {
                if (UsingX)
                {
                    return SliderRectangle.X - TrackRectangle.X;
                }
                else
                {
                    return SliderRectangle.Y - TrackRectangle.Y;
                }
            }
        }
        public int MinX
        {
            get { return TrackRectangle.X; }
        }
        public int MaxX
        {
            get { return TrackRectangle.X + TrackRectangle.Width - SliderRectangle.Width; }
        }
        public int MinY
        {
            get { return TrackRectangle.Y; }
        }
        public int MaxY
        {
            get { return TrackRectangle.Y + TrackRectangle.Height - SliderRectangle.Height; }
        }

        private bool moving = false;
        public virtual void Update()
        {
            float hundred = Porcentage;
            Vector2 mousePos = Engine.Instance.InputManager.GetMousePosition();
            Rectangle mouseRect = new Rectangle((int)mousePos.X, (int)mousePos.Y, 1, 1);
            if (mouseRect.Intersects(SliderRectangle) &&
                Engine.Instance.InputManager.IsMouseLeftClick())
            {
                moving = true;
            }
            if (Engine.Instance.InputManager.IsMouseLeftUp())
            {
                moving = false;
            }

            if (moving)
            {
                if (Minimum < Maximum)
                {
                    if (UsingX)
                    {
                        float X = mouseRect.X;
                        X = MathHelper.Clamp(X, MinX, MaxX);
                        SliderRectangle = new Rectangle((int)X, SliderRectangle.Y, SliderRectangle.Width, SliderRectangle.Height);

                        float Size = MaxX - MinX;
                        float Factor = Maximum / Size;
                        currentValue = MathHelper.Clamp((X - MinX) * Factor, Minimum, Maximum);

                        if (OnSlide != null)
                        {
                            OnSlide(this.currentValue);
                        }

                    }
                    else
                    {
                        float Y = mouseRect.Y;
                        Y = MathHelper.Clamp(Y, MinY, MaxY);
                        
                        int X = RectangleUtil.CenterRectangleX(SliderRectangle, TrackRectangle.Width);
                        SliderRectangle = new Rectangle(X + TrackRectangle.X, (int)Y, SliderRectangle.Width, SliderRectangle.Height);


                        float Size = MaxY - MinY;
                        float Factor = Maximum / Size;
                        currentValue = MathHelper.Clamp((Y - MinY) * Factor, Minimum, Maximum);

                        if (OnSlide != null)
                        {
                            OnSlide(this.currentValue);
                        }
                    }
                }
            }
            else
            {
                if (UsingX)
                {
                    int Y = RectangleUtil.CenterRectangleX(SliderRectangle, TrackRectangle.Width);
                    SliderRectangle = new Rectangle(SliderRectangle.X, Y + TrackRectangle.Y, SliderRectangle.Width, SliderRectangle.Height);
                }
                else
                {
                    int X = RectangleUtil.CenterRectangleX(SliderRectangle, TrackRectangle.Width);
                    SliderRectangle = new Rectangle(X + TrackRectangle.X, SliderRectangle.Y, SliderRectangle.Width, SliderRectangle.Height);
                }
            }
        }
    }
}
